package org.edu.modules.sxjy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.modules.sxjy.common.noresubmit.CacheLock;
import org.edu.modules.sxjy.entity.VeDoc;
import org.edu.modules.sxjy.service.CacheKeyGenerator;
import org.edu.modules.sxjy.service.IVeDocService;
import org.edu.modules.sxjy.service.impl.CacheKeyGeneratorImp;
import org.edu.modules.sxjy.utils.PageResponseResult;
import org.edu.modules.sxjy.vo.VeDocVo;
import org.edu.modules.sxjy.vo.VeDocVos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags={"实习前期相关制度文档"})
@RestController
@RequestMapping({"/sxjy/veDoc"})
@ApiSort(60)
public class VeDocController
        extends BaseController<VeDoc, IVeDocService>
{
    private static final Logger log = LoggerFactory.getLogger(VeDocController.class);
    @Autowired
    private IVeDocService veDocService;

    @Bean
    public CacheKeyGenerator cacheKeyGenerator()
    {
        return new CacheKeyGeneratorImp();
    }

    @AutoLog("实习前期相关制度文档-通过栏目编码查询")
    @ApiOperation(value="通过栏目编码查询", notes="实习前期相关制度文档-通过栏目编码查询")
    @GetMapping({"/selectCateCode"})
    public PageResponseResult selectCateCode(VeDocVos veDocVos, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize)
    {
        Integer startLine = Integer.valueOf((pageNo.intValue() - 1) * pageSize.intValue());
        List<VeDocVos> veDocList = this.veDocService.selectCateCode(veDocVos, startLine, pageSize);
        int total = this.veDocService.getSumPage(veDocVos);
        if ((Objects.isNull(veDocList)) && (veDocList.size() <= 0)) {
            return new PageResponseResult(Integer.valueOf(1), 1, 0, Result.OK());
        }
        double totals = total;
        double pageSizes = pageSize.intValue();
        double totalPage = Math.ceil(totals / pageSizes);
        return new PageResponseResult(pageNo, (int)totalPage, total, Result.OK(veDocList));
    }

    @AutoLog("实习前期相关制度文档-通过id查询")
    @ApiOperation(value="通过id查询", notes="实习前期相关制度文档-通过id查询")
    @GetMapping({"/queryById"})
    public Result<?> queryById(@RequestParam(name="id", required=true) String id)
    {
        VeDoc veDoc = (VeDoc)this.veDocService.getById(id);
        if (veDoc == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(veDoc);
    }

    @AutoLog("实习前期相关制度文档-新增信息")
    @ApiOperation(value="新增信息", notes="实习前期相关制度文档-新增信息")
    @PostMapping({"/add"})
    public Result<?> add(@RequestBody VeDocVo veDocVo)
    {
        if (veDocVo != null)
        {
            this.veDocService.saves(veDocVo);
            return Result.OK("添加成功！");
        }
        return Result.error("添加失败");
    }

    @AutoLog("实习前期相关制度文档-修改信息")
    @ApiOperation(value="修改信息", notes="实习前期相关制度文档-修改信息")
    @PutMapping({"/edit"})
    @CacheLock(prefix="redisLock.edit", expire=1)
    public Result<?> edit(@RequestBody VeDocVo veDocVo)
    {
        if (veDocVo != null)
        {
            this.veDocService.edit(veDocVo);
            return Result.OK("编辑成功!");
        }
        return Result.error("编辑失败");
    }

    @AutoLog("实习前期相关制度文档-通过id删除")
    @ApiOperation(value="通过id删除", notes="实习前期相关制度文档-通过id删除")
    @DeleteMapping({"/delete"})
    public Result<?> delete(@RequestParam(name="id", required=true) String id)
    {
        this.veDocService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog("实习前期相关制度文档-批量删除")
    @ApiOperation(value="批量删除", notes="实习前期相关制度文档-批量删除")
    @DeleteMapping({"/deleteBatch"})
    public Result<?> deleteBatch(@RequestParam(name="ids", required=true) String ids)
    {
        this.veDocService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @AutoLog("实习前期相关制度文档-编辑回显")
    @ApiOperation(value="编辑回显", notes="实习前期相关制度文档-编辑回显")
    @GetMapping({"/udpateEcho"})
    public Result<?> updateEcho(int id)
    {
        if (id != 0)
        {
            VeDocVo veDocVo = this.veDocService.editEcho(id);
            return Result.OK(veDocVo);
        }
        return Result.error("编辑失败");
    }
}
